package com.ican.dao.user;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.ican.common.hibernate.HibernateDao;
import com.ican.common.hibernate.Page;
import com.ican.domain.user.User;
import com.ican.util.MD5Util;

@Repository("userDao")
public class UserDao extends HibernateDao<User, Integer> {
    
    public User findUserByNameAndPw(String name, String pwd){
        pwd = MD5Util.Md5(pwd);
        return this.findUnique("from User where name = ? and password = ?", new Object[]{name, pwd});
    }
    
    public User findUserById(Integer id){
        return this.findUnique("from User where id = ?", id);
    }
    
    public boolean userNameIsUnique(String name){
        Long i = findUnique("select count(id) from User where name = ?", name);
        if(i > 0){
            return false;
        }
        return true;
    }
    
    public boolean userNameIsUnique(Integer id, String name){
        Long i = findUnique("select count(id) from User where name = ? and id != ?", new Object[]{name,id});
        if(i > 0){
            return false;
        }
        return true;
    }
    
    public Page<User> findAllUser(Page<User> page, User user){
        StringBuffer sb = new StringBuffer("from User");
        if(user != null){
            if(user.getName() != null && user.getName().length() > 0){
                sb.append(" where name like '%" + user.getName() + "%'");
            }
        }
        return this.findPage(page, sb.toString());
    }
    
    /**
     * @param dataStatus    0:可用；1：不可用; 其他（-1）：全部
     */
    public List<User> findAllUser(int dataStatus){
        String hql = null;
        if(dataStatus == 0){
            hql = "from User where dataStatus = 0";
        }else if(dataStatus == 1){
            hql = "from User where dataStatus = 1";
        }else{
            hql = "from User";
        }
        return this.find(hql);
    }
    
    public void addUser(User user){
        user.setPassword(MD5Util.Md5(user.getPassword()));
        user.setCreateTime(new Date());
        this.save(user);
    }
    
    public void modifyUserNameAndPw(User user){
        String hql = "update User u set u.name = ?, u.disName = ?";
        if(user.getPassword() != null && user.getPassword().length() > 0){
            hql = hql + ", u.password = ?, u.modifyTime = ? where u.id = ?";
            this.batchExecute(hql, new Object[]{user.getName(), user.getDisName(), MD5Util.Md5(user.getPassword()), new Date(), user.getId()});
        }else{
            hql = hql + ", u.modifyTime = ? where u.id = ?";
            this.batchExecute(hql, new Object[]{user.getName(), user.getDisName(), new Date(), user.getId()});
        }
    }
    
    public void modifyUserStatus(Integer userId, byte dataStatus){
        String hql = "update User u set u.dataStatus = ?, u.modifyTime = ? where u.id = ?";
        this.batchExecute(hql, new Object[]{dataStatus, new Date(), userId});
    }
}
